// 多语言方法
var lang = function(tag) {
    return window.languageResource[tag] || '';
};
$(function () {
    // 常用正则
    window.commonRegexp = {
        'mobile': /^.{1,}$/,
        'password': /^(?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9]{6,20}$/,
        'username': /^[a-zA-Z0-9_u4e00-u9fa5]+$/,
        'hotelName': /^.{1,20}$/,
        'name': /^.{1,20}$/,
        'cityName': /^.{1,20}$/,
        'address': /^.{1,255}$/,
        'vcode': /^\w{4}$/,
        'smsvcode': /^\w{6}$/,
        'identity':/^[a-zA-z0-9]{0,18}$/,
        'lenght':/^\S{1,100}$/,
        'imageCode':/^[0-9a-zA-Z]{4}$/
    };
    // 图片懒加载
    if ($('img.lazyload').lazyload !== undefined) {
        $('img.lazyload').lazyload({
            skip_invisible: false,
            failure_limit : 10,
            effect: 'fadeIn'
        });
        $('div.lazyload-background').lazyload({
            skip_invisible: true,
            failure_limit : 10,
            effect: 'fadeIn'
        });
    }

    $('#qrcode-container-trigger').on('mouseover', function(){
        $('#qrcode-container').css('display', 'block');
        $(window).trigger("scroll");
    }).on('mouseout', function(){
        $('#qrcode-container').css('display', 'none');
    });
    // 初始化区号选择
    var areaCode;
    if ($.cookie("area_code_cookie") != 'undefined') {
        areaCode = $.cookie("area_code_cookie");
        if (areaCode != undefined) {
            $('.areaCode').find('.areaCodeValue').text(areaCode);
        } else {
            $('.areaCode').find('.areaCodeValue').text('86');
        }
    }  
    $('.areaCode').on('click', '.dropdown-menu a', function() {
        var me = $(this),
        value = me.data('value');
        me.parents('.areaCode').find('.areaCodeValue').text(value);
        if ($.cookie("area_code_cookie") != 'undefined') {
            $.cookie('area_code_cookie', value, { expires: 7, path: '/' });
        }
    });     
    //设置语言cookie
    $(".languageSwitch").find("a").click(function(){
       $.cookie('mz_think_language', $(this).attr('data-language'), { expires: 7, path: '/' });
       //重载当前页面
       window.location.reload();
    });
});
//ajax异步请求
/* exported ajaxRequest */
var ajaxRequest = function (url, params, method, successcallback, errorcallback, obj) {
    if (method === null || method === '') {
        method = 'get';
    }
    $.ajax({
        url: url,
        data: params,
        type: method,
        cache: false,
        dataType: 'json',
        success: function (data) {
            if (obj !== undefined) {
                obj.removeAttr('disabled');
            }
            if (typeof successcallback == 'function') {
                successcallback(data, obj);
            } else {
                window.location.reload();
            }
        },
        error: function (error) {
            if (obj !== undefined) {
                obj.removeAttr('disabled');
            }
            if (typeof errorcallback == 'function') {
                errorcallback(error, obj);
            } else {
                if (typeof error.responseJSON == 'undefined') {
                    formInlineTip(obj, lang('tip.server'));
                } else {
                    formInlineTip(obj, error.responseJSON.info);
                }
            }
        }
    });
};
//延时跳转到url
/* exported timeoutForward */
var timeoutForward = function (url, time) {
    if (url === undefined || url === '') {
        url = window.location.href;
    }
    if (time === undefined) {
        time = 1500;
    }
    window.setTimeout(function () {
        window.location.href = url;
    }, time);
};
//按钮等待
/* exported timeDisplay */
var timeDisplay = function (objbtn, time) {
    objbtn.addClass('disabled');
    objbtn.attr('disabled', true);
    var i = time;
    var id = window.setInterval(function () {
        i = i - 1;
        objbtn.find('.displayTime').text('(' + i + ')');
        if (i === 0) {
            window.clearInterval(id);
            objbtn.find('.displayTime').text('');
            objbtn.removeClass('disabled');
            objbtn.removeAttr('disabled');
        }
    }, 1000);
};
// 刷新验证码
/* exported refreshVcode */
var refreshVcode = function () {
    var imgCodeObj = $('#imgcode');
    var vcodeUrl = imgCodeObj.attr('src');
    var indexOfQuestionMark = vcodeUrl.indexOf('?');
    if (indexOfQuestionMark !== -1) {
        vcodeUrl = vcodeUrl.substr(0, indexOfQuestionMark);
    }
    var time = new Date().getTime();
    imgCodeObj.attr('src', vcodeUrl + '?' + time);
};
/* exported formInlineTip */
var formInlineTip = function (obj, info, type) {
    if (type === undefined) {
        type = 0;
    }
    // 检测data中是否定义了显示提示的另一个位置
    var tipObjectStr = $(obj).data('tip-object');
    var tipObject = $(tipObjectStr);
    var hasTipObject = tipObject.length !== 0;
    if (!hasTipObject) {
        tipObject = obj;
    }
    var formGroupObj = tipObject.parents('.form-group');
    var helpBlock = formGroupObj.find('.help-block');
    helpBlock.html(info);
    var tipClass = '';
    switch (type) {
        case 2:
            tipClass = 'has-warning';
            break;
        case 1:
            tipClass = 'has-success';
            break;
        case 0:
            tipClass = 'has-error';
            break;
        default:
            break;
    }
    // 根据tip的类型改变help颜色
    formGroupObj.addClass(tipClass);
    if (hasTipObject) {
        obj.parent().addClass(tipClass);
    }
};